#!/bin/sh

LUA=lua

echo "Starting philosopher's server"
$LUA philo.lua &
pids="$pids $!"
trap "kill $pids > /dev/null 2> /dev/null" 0

for((i=0;i<10;++i));do if test -r philo.ior;then break;else sleep 1;fi;done

echo "Starting fork server"
$LUA fork.lua &
pids="$pids $!"
trap "kill $pids > /dev/null 2> /dev/null" 0

for((i=0;i<10;++i));do if test -r fork.ior;then break;else sleep 1;fi;done

echo "Starting observer server"
$LUA observer.lua &
pids="$pids $!"
trap "kill $pids > /dev/null 2> /dev/null" 0

for((i=0;i<10;++i));do if test -r observer.ior;then break;else sleep 1;fi;done

echo "Configuring and starting application"
$LUA config.lua

echo "Execute for 30 secs. for a deadlock situation"
sleep 30

echo "Adapt application to prevent deadlocks"
$LUA adapt.lua

echo "Execute for more 30 secs."
sleep 30

echo "Demo finished."
rm -f philo.ior fork.ior observer.ior
